package dhome;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;
import dhome.util.Host;

import net.pms.PMS;
import net.pms.configuration.PmsConfiguration;
import net.pms.logging.LoggingConfigFileLoader;

/**
 * export LOGBACK_CONFIG_FILE_LOCATION=/tmp/logback.conf java
 * -Dlogback.configurationFile=${LOGBACK_CONFIG_FILE_LOCATION}
 * 
 */
public class Application {
	static Logger log = LoggerFactory.getLogger(Application.class.getName());

	public Application() {		
		log.info("Start {}","Application");
		init();
	}

	/**
	 * Check if server is running in headless (console) mode.
	 * 
	 * @return true if server is running in headless (console) mode, false
	 *         otherwise
	 */
	public static synchronized boolean isHeadless() {
		if (System.getProperty("console") != null) {
			return true;
		} else {
			try {
				javax.swing.JDialog d = new javax.swing.JDialog();
				d.dispose();
				return false;
			} catch (Throwable throwable) {
				return true;
			}
		}
	}

	void init() {
		// 初始化定时任务
		// 初始化系统Watcher
		// 初始化vaadin
		// 初始化PMS
		System.setProperty("java.io.tmpdir", "./tmp");
		System.setProperty("XDG_CONFIG_HOME", "./conf");
		log.debug("init");
		try {
			PMS.setConfiguration(new PmsConfiguration());
			LoggingConfigFileLoader.load();

			PMS.createInstance();
		} catch (Throwable t) {
			t.printStackTrace();
		}

	}

	void startup() {

	}

	void destroy() {

	}

	void start_jetty() {

	}

	public static void main(String[] args) throws InterruptedException {
		log.debug("HOME= {} ",Host.HOME);
		LoggerContext context = (LoggerContext) LoggerFactory
				.getILoggerFactory();
		JoranConfigurator configurator = new JoranConfigurator();
		configurator.setContext(context);
		context.reset();
		try {
			configurator.doConfigure(System.getProperty("DS_HOME")
					+ "/conf/logback.xml");
		} catch (JoranException e) {
			e.printStackTrace();
		}
		new Application();
		// Thread.currentThread().join();
	}

}
